package com.example.uniapp.activity.main;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.uniapp.R;
import com.example.uniapp.activity.userHome.CookbookUserHome;
import com.example.uniapp.bean.CookbookBean;
import com.example.uniapp.bean.CookbookFoodBean;
import com.example.uniapp.bean.CookbookStepBean;
import com.example.uniapp.bean.UserBean;
import com.example.uniapp.dao.CookbookFoodDao;
import com.example.uniapp.dao.CookbookStepDao;
import com.example.uniapp.dao.UserDao;

import java.util.List;

public class CookbookShowActivity extends AppCompatActivity {
    private LinearLayout cookbookFoodLayout;
    private LinearLayout cookbookStepLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cookbook_show);

        cookbookFoodLayout = this.findViewById(R.id.user_each_cookbook_food);
        cookbookStepLayout = this.findViewById(R.id.user_each_cookbook_step);

        //获取界面中的各个组件
        Toolbar toolbar = this.findViewById(R.id.user_each_cookbook_toolbar);
        ImageView imgView = this.findViewById(R.id.user_each_cookbook_img);
        ImageView avatorImgView = this.findViewById(R.id.user_each_cookbook_user_avator);
        EditText userNameText = this.findViewById(R.id.user_each_cookbook_user_name);
        EditText nameText = this.findViewById(R.id.user_each_cookbook_name);

        //获取数据
        Intent intent = getIntent();
        CookbookBean cookbookBean = (CookbookBean) intent.getSerializableExtra("cookbook");

        //获取该食谱发布人信息
        UserBean userBean = UserDao.getUserInfoByAccount(cookbookBean.getUserId());
        Bitmap userImgBitmap = BitmapFactory.decodeFile(userBean.getUserImg());
        avatorImgView.setImageBitmap(userImgBitmap);
        userNameText.setText(userBean.getUserId());

        //载入数据
        Bitmap cookbookBitmap = BitmapFactory.decodeFile(cookbookBean.getCookbookImg());
        imgView.setImageBitmap(cookbookBitmap);
        nameText.setText(cookbookBean.getCookbookName());

        //获取食材数据并载入
        List<CookbookFoodBean> cookbookFoodBeanList = CookbookFoodDao.getCookbookFoodByCookbookId(cookbookBean.getCookbookId());
        for(int i = 0; i < cookbookFoodBeanList.size(); i++) {
            addFoodInputItem(cookbookFoodBeanList.get(i));
        }

        //获取制作步骤并载入
        List<CookbookStepBean> cookbookStepBeanList = CookbookStepDao.getCookbookStepByCookbookId(cookbookBean.getCookbookId());
        for(int i = 0; i < cookbookStepBeanList.size(); i++) {
            addStepInputItem(cookbookStepBeanList.get(i));
        }

        //设置点击事件
        //返回功能
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //返回
                finish();
            }
        });
        //保存该食谱发布人的信息并跳转到他的个人空间
        avatorImgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CookbookShowActivity.this, CookbookUserHome.class);
                intent.putExtra("userInfo", userBean);
                startActivity(intent);
            }
        });
        userNameText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CookbookShowActivity.this, CookbookUserHome.class);
                intent.putExtra("userInfo", userBean);
                startActivity(intent);
            }
        });
    }

    private void addFoodInputItem(CookbookFoodBean cookbookFoodBean) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View itemView = inflater.inflate(R.layout.itemview_food_show_layout, cookbookFoodLayout, false);

        //获取组件
        EditText foodNameEditText = itemView.findViewById(R.id.itemview_food_show_layout_food_name);
        EditText foodCountEditText = itemView.findViewById(R.id.itemview_food_show_layout_food_count);

        //填充数据
        foodNameEditText.setText(cookbookFoodBean.getCookbookFood());
        foodCountEditText.setText(cookbookFoodBean.getCookbookFoodCount());

        //添加到布局
        cookbookFoodLayout.addView(itemView);
        cookbookFoodLayout.requestLayout();
    }

    private void addStepInputItem(CookbookStepBean cookbookStepBean) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View itemView = inflater.inflate(R.layout.itemview_step_show_layout, cookbookStepLayout, false);

        //获取组件
        EditText stepEditText = itemView.findViewById(R.id.itemview_step_show_layout_step);

        //填充数据
        stepEditText.setText(cookbookStepBean.getCookbookStep());

        //添加到布局
        cookbookStepLayout.addView(itemView);
        cookbookStepLayout.requestLayout();
    }
}